package com.mystyle.le.qjnewsproject.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mystyle.le.qjnewsproject.MyAsyncTaks.TopAsyncTaks;
import com.mystyle.le.qjnewsproject.R;
import com.mystyle.le.qjnewsproject.beans.TopAd;
import com.mystyle.le.qjnewsproject.interfacecallback.CallBack;

import java.util.List;

/**
 * Created by le on 2016/10/8.
 */
public class TopPicAdapter extends BaseAdapter {
    private List<ImageView> list;
    private Context context;
    private List<TopAd> topAds;

    public TopPicAdapter(List<ImageView> list, Context context, List<TopAd> topAds) {
        this.list = list;
        this.context = context;
        this.topAds = topAds;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView==null){

            convertView = LayoutInflater.from(context).inflate(R.layout.piclayout,parent,false);
            holder = new ViewHolder();
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        TopAd topAd = topAds.get(position);
        final String title = topAd.getTitle();
        String icon = topAd.getIcon();
        new TopAsyncTaks(new CallBack.GetPicture() {
            @Override
            public void getPicture(Bitmap bitmap) {
                holder.textView.setText(title);
                ImageView imageView = new ImageView(context);
                imageView.setImageBitmap(bitmap);

                
            }
        }).execute(icon);
        return convertView;
    }
    class ViewHolder{
        TextView textView;
    }
}
